import { Article } from './search'

// 注意设计格式
// 🎯 文章集合格式设计：根据频道id存储对应文章列表
export type Articles = {
  //  [] 动态设置属性名
  [channel_id: number]: {
    timestamp: number
    list: Article[]
  }
}

export type ArticleMark = {
  visible: boolean
  article_id: string
}

export type ArticleDetail = {
  art_id: string
  title: string
  pubdate: string
  aut_id: string
  content: string
  aut_name: string
  aut_photo: string
  is_followed: boolean
  is_collected: boolean
  attitude: number
  comm_count: number
  read_count: number
  like_count: number
}

// 初始数据
const initState = {
  mark: { visible: false, article_id: '' } as ArticleMark,
  articles: {} as Articles,
  detail: {} as ArticleDetail,
}

// 定义 action 数据类型
export type ArticleAction =
  | { type: 'article/saveArticles'; payload: Articles }
  | { type: 'article/setMark'; payload: ArticleMark }
  | { type: 'article/saveDetail'; payload: ArticleDetail }

export const articleReducer = (state = initState, action: ArticleAction) => {
  switch (action.type) {
    case 'article/saveArticles':
      return { ...state, articles: action.payload }
    case 'article/setMark':
      return { ...state, mark: action.payload }
    case 'article/saveDetail':
      return { ...state, detail: action.payload }
    default:
      return state
  }
}
